<?php
/* **************************************************************************
File:					image.estategent.php
EstateAgent Improved - a Joomla Component

Copyright:			2004 - 2006 by Darko Selesi

Year: 2007
Improved by: Manos Orfanoudakis - "vosk"
Homepage: 		http://www.eaimproved.eu
Version: 			1.3.x                     
License:			GNU/GPL http://www.gnu.org/copyleft/gpl.html
*************************************************************************** */


defined( '_VALID_MOS' ) or die( 'Direct Access to this location is not allowed.' );


class EAImage extends mosDBTable {

	var $id = null;
  	var $objid = null;
  	var $title = null;
  	var $description = null;
  	var $fname = null;
  	var $type = null;
  	var $path = null;
  	var $owner = null;
  	var $ordering = null;
  	var $published = null;
  	  	
	function EAImage( &$db ) {
    	$this->mosDBTable( '#__estateagent_images', 'id', $db );
  	}
  	
  	function load_firstPic($oid, $path, $pwidth){
  		global $database, $mosConfig_absolute_path, $mosConfig_live_site;
  		
		$html = "";
		$file = "nopic.png";
		$picpath = $path; 		  		
  		$p = null;

		$query = "SELECT title,fname,type,path FROM #__estateagent_images WHERE objid = $oid AND (type = '.jpg' OR type = '.gif' OR type = '.png') AND ordering = 1 AND published = 1";
  		$database->setQuery($query);
  		$database->loadObject($p);
  		
		if($p != null) {
				$picpath = ($p->path == "")? $picpath : $p->path;
				$p1 = "t" . $p->fname.$p->type;
				$p2 = $p->fname.$p->type;
				$file = (file_exists($mosConfig_absolute_path.$picpath.$p2))? $p2 : (file_exists($mosConfig_absolute_path.$picpath.$p1))? $p1 : $file;			
				$html =	"\n\t<img src='".$mosConfig_live_site.$picpath.$file."'  border='0' width='".$pwidth."' alt='$file'/>";
  		}
		else {
				/*#echo("<script>alert('Path ".$mosConfig_absolute_path.$picpath."');</script>");*/
				$picpath = $mosConfig_live_site . "/components/com_estateagent/pictures/nopic.png";
				$html =	"\n\t<img src='".$picpath."'  border='0' width='".$pwidth."' alt=''/>";
		}
		
		
		return $html;
  	} 
  	
	//overwritten standard delete method 
	function delete($oid=null){
		global $mosConfig_absolute_path, $database;

		if($oid){
			$this->id = $oid;
		}
		// is the image file linked to another object?
		$query = "SELECT id FROM #__estateagent_images WHERE fname = '".$this->fname."' AND id != '".$this->id."'";
		$this->_db->setQuery($query);
  		if($this->_db->loadResult() == null) {	
  			if(chdir($mosConfig_absolute_path.$this->path)) {
	    		$img = $this->fname.$this->type;
    			$thumb = "t".$this->fname.$this->type;
	    		@chmod($img, 0777);
    			@chmod($thumb, 0777);
    			unlink($img);
				echo $thumb;
				unlink($thumb);
    		}
		}
		 
        $query = "DELETE FROM #__estateagent_images"
        			. " WHERE id = '". $this->id ."'";
        $this->_db->setQuery( $query );
  
        if ($this->_db->query()) {
        	return true;
        } else {
        	$this->_error = $this->_db->getErrorMsg();
            return false;
        }	
	}
}





?>
